﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using  DHQGHN.DAL.Base;
using  DHQGHN.DAL.Common;
using  DHQGHN.DAL.Models.Admin;

namespace  DHQGHN.DAL.DAO.Admin
{
    public class GroupDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING_GROUP = "spGetListPagingGroup";
        public const string SP_GET_LIST_PAGING_GROUP_D = "spGetListPagingGroupd";
        public const string SP_GET_ALL_GROUP = "spGetAllGroup";
        public const string SP_GET_LIST_PAGING_GROUP_BY_ID = "spGetGroupById";
        public const string SP_CREATE_OR_UPDATE_GROUP = "spCreateOrUpdateGroup";
        public const string SP_GET_ALL_GROUP_BY_USERID_APPCODE = "spGetAllGroupByUserIdAndAppCode";
        public const string SP_DELETE_GROUP = "spDeleteGroup";
        public const string SP_CHECK_GROUP_EXIST = "spCheckGroupExist";
        public const string SP_CHECK_EXIST_GROUP_CODE = "spCheckExistGroupCode";
        public const string SP_GROUP_NAME_BY_GROUP_ID = "spGetGroupNameByGroupId";
        public const string SP_GROUP_CODE_BY_GROUP_ID = "spGetGroupCodeByGroupId";
        public const string SP_RE_ACTIVE_GROUP = "spReActiveGroup";
        public const string SP_GET_GROUP_LIST_BY_APP_ID = "spGetGroupListByAppId";
        public const string SP_GET_GROUP_BY_APP_CODE = "spGetAllGroupAppCode";

        public const string Param_GROUP_ID = "@GroupId";
        public const string Param_NAME = "@GroupName";
        public const string Param_CODE = "@GroupCode";
        public const string Param_APPID = "@AppId";
        public const string Param_TYPE = "@Type";
        public const string Param_UserId = "@UserId";
        public const string Param_AppCode = "@AppCode";
        public const string Param_IsActive = "@IsActive";
        public const string Param_App_Code = "@AppCode";

        public GroupDAO(String connectionString, System.Type type = null)
            : base(connectionString, type) { ;}

        internal DataPage<GroupModel> GetDynamicListPaging(GroupModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary, bool compareEqual)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_GROUP);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_NAME,
                        compareEqual ? model.GroupName : string.IsNullOrEmpty(model.GroupName) ? model.GroupName : ("%" + model.GroupName + "%")));
                    command.Parameters.Add(CreateParameter(Param_CODE,
                        compareEqual ? model.GroupCode : string.IsNullOrEmpty(model.GroupCode) ? model.GroupCode : ("%" + model.GroupCode + "%")));
                    command.Parameters.Add(CreateParameter(Param_APPID, model.AppId));
                    command.Parameters.Add(CreateParameter(Param_TYPE,
                        compareEqual ? model.Type : string.IsNullOrEmpty(model.Type) ? model.Type : ("%" + model.Type + "%")));
                    command.Parameters.Add(CreateParameter(Param_IsActive, model.IsActive));

                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<GroupModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal GroupModel GetById(int? appId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_GROUP_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_GROUP_ID, appId));

                return GetSingle<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateGroup(GroupModel appModel, int[] nhomCoBan)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_GROUP);
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_GROUP_ID, appModel.GroupId));
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_NAME, appModel.GroupName));
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_CODE, appModel.GroupCode));
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_APPID, appModel.AppId));
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_TYPE, appModel.Type));
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_DESCRIPTIONS, appModel.Description));
                DataTable tableNhomCoBan = GetTableIdNhomCoBan(nhomCoBan);
                command.Parameters.Add(new SqlParameter("@TableNhomCoBan", tableNhomCoBan));
                //command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_DELETED, appModel.IsActive));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteGroup(int[] appId)
        {
            try
            {
                //DataTable tblGroupList = new DataTable();
                //tblGroupList.Columns.Add("GroupId");
                //foreach (int id in appId)
                //{
                //    tblGroupList.Rows.Add(id);
                //}

                SqlCommand command = this.GetSprocCommand(SP_DELETE_GROUP);
                command.Parameters.Add(new SqlParameter("@GroupList", GetTableId<int>(appId, "GroupId")));
                //command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_GROUP_ID, appId));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal List<CheckBoxList> GetAllByUserName(string UserName, string AppCode, Dictionary<string, string> mapDictionary)
        {
            try
            {
                if (UserName == "1")
                    UserName = "";
                SqlCommand command = this.GetSprocCommand("spGetAllGroupUserName");
                command.Parameters.Add(new SqlParameter("@UserName", UserName));
                command.Parameters.Add(new SqlParameter("@AppCode", AppCode));
                return GetList<CheckBoxList>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<GroupModel> GetAllAppCode(string AppCode, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_GROUP_BY_APP_CODE);
                command.Parameters.Add(CreateParameter(Param_App_Code, AppCode));

                return GetList<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<GroupModel> GetAll(int? AppId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_GROUP);
                command.Parameters.Add(new SqlParameter("@AppId", AppId));
                return GetList<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal IEnumerable<GroupModel> GetAllByUserIdAndAppCode(int userId, string appCode, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_GROUP_BY_USERID_APPCODE);
                command.Parameters.Add(new SqlParameter(Param_UserId, userId));
                command.Parameters.Add(new SqlParameter(Param_AppCode, appCode));
                return GetList<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool CheckGroupExist(string _groupcode)
        {

            SqlCommand command = this.GetSprocCommand(SP_CHECK_GROUP_EXIST);
            command.Parameters.Add(CreateParameter(Param_CODE, _groupcode));
            int tmp = GetSingleInt32(command);
            return tmp == 1 ? true : false;
        }

        internal bool CheckGroupExist(string _groupCode, string _appCode)
        {

            SqlCommand command = this.GetSprocCommand(SP_CHECK_EXIST_GROUP_CODE);
            command.Parameters.Add(CreateParameter(Param_CODE, _groupCode));
            command.Parameters.Add(CreateParameter(Param_App_Code, _appCode));
            int tmp = GetSingleInt32(command);
            return tmp == 1 ? true : false;
        }

        internal string GetGroupNameByGroupId(int? groupId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GROUP_NAME_BY_GROUP_ID);
                command.Parameters.Add(new SqlParameter(Param_GROUP_ID, groupId));
                string tmp = GetSingleString(command);
                return tmp;
                //return GetList<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal string GetGroupCodeByGroupId(int? groupId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GROUP_CODE_BY_GROUP_ID);
                command.Parameters.Add(new SqlParameter(Param_GROUP_ID, groupId));
                string tmp = GetSingleString(command);
                return tmp;
                //return GetList<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal DataPage<GroupModel> GetDynamicListPagingd(GroupModel model, int pageSize, int pageIndex, Dictionary<string, string> mapDictionary, bool compareEqual)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_GROUP_D);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_NAME, compareEqual ? model.GroupName : ("%" + model.GroupName + "%")));
                    command.Parameters.Add(CreateParameter(Param_CODE, compareEqual ? model.GroupCode : ("%" + model.GroupCode + "%")));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<GroupModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool reActive(int appId)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_RE_ACTIVE_GROUP);
                command.Parameters.Add(new SqlParameter("@" + GroupModel.FIELD_GROUP_ID, appId));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// quyvd, 20150827, lay theo groupcode
        /// </summary>
        /// <param name="enumCode"></param>
        /// <returns></returns>
        public List<ActionGroupCode> GetAllUserGroupCode(string GROUPCODE)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("[dbo].[spGetAllActionNameByGroupCode]");
                cmd.Parameters.Add(CreateParameter(Param_CODE, GROUPCODE));
                return GetList<ActionGroupCode>(cmd);
            }
            catch
            {
                throw;
            }
        }

        internal List<GroupModel> GetListGroupByApp(int AppID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_GROUP_LIST_BY_APP_ID);
                command.Parameters.Add(CreateParameter(Param_APPID, AppID));
                return GetList<GroupModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<GroupModel> GetListGroupByAppIdAndNhomCoBanId(int appId, int nhomCoBanId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spGetGroupListByAppId_And_NhomCoBanId");
                cmd.Parameters.Add(CreateParameter(Param_APPID, appId));
                cmd.Parameters.Add(CreateParameter("@NhomCoBanId", nhomCoBanId));

                return GetList<GroupModel>(cmd, mapDictionary);
            }
            catch
            {
                
                throw;
            }
        }

        private DataTable GetTableIdNhomCoBan(int[] nhomCoBan)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            for (int i = 0; i < nhomCoBan.Length; i++)
            {
                table.Rows.Add(nhomCoBan[i]);
            }

            return table;
        }
    }
}
